// functions.js

// Allows just numbers for a field
function checkIt(evt) 
{
    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) 
        return false;
    return true;
}

function isControlChar(charCode){
	if(charCode == 8
		|| charCode == 9 
		|| charCode == 16
		|| charCode == 35
		|| charCode == 36
		|| charCode == 37
		|| charCode == 38
		|| charCode == 39
		|| charCode == 40
	)
		return true;
}


function checkItNameString(evt)
{
    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode;
 
    alert(charCode);
    
    if(isControlChar(charCode))
    	return true;
    
    if(charCode == 91 || charCode == 92 || charCode == 93)
    
    if(checkIt(evt))
    	return false;
    
    if((charCode > 31 && (charCode < 65 || charCode > 122)) && charCode != 32) 
        return false;
    
    return true;
}

function checkItAddressString(evt)
{
    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    
    if(isControChar(charCode))
    	return true;
    
    if(checkIt(evt))
    	return true;

    if((charCode > 31 && (charCode < 65 || charCode > 122)) 
    		&& charCode != 32
    		&& charCode != 95
    		&& charCode != 45
    		&& charCode != 46
    		&& charCode != 35) 
        return false;
    
    return true;
}



function checkEnter(e) //e is event object passed from function invocation
{ 
	var characterCode; //literal character code will be stored in this variable
	
	if(e && e.which)
	{ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else
	{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}

	if(characterCode == 13)
		return true
	else
		return false;
}

function trim(s)
{
	var l=0; var r=s.length -1;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	while(r > l && s[r] == ' ')
	{	r-=1;	}
	return s.substring(l, r+1);
}

function hasLeadingZero(n)
{
	var fChar = n.substring(0,1);
	return (fChar=='0') ? true : false;
}

function removeLeadingZeros(n)
{
	for(i=0; i<n.length; i++)
		if(n.charAt(i) != 0)
			return n.substring(i, n.length);

	return n;
}

/*
 * Add the specified number of leading zeros 
 * to a field containing a number.
 */
function leadingZeros(x, fieldSize)
{
	if(x.length == 0)
		return '';

	var xc = removeLeadingZeros(x);
	var zeros = '';
	for(i=0; i<(fieldSize-xc.length); i++)
		zeros += '0';
		
	return zeros += xc;	
}	


/*
 * Returns true if the argument
 * is recognized as e-mail.
 */ 
function isEmail(thaImeu) 
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(thaImeu))
		return true;
	return false;
}

/*
 * Returns true if the parameter is a valid
 * US phone number, like (###) ###-####
 * (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
 */
function isPhoneNumber(s) 
{
	// Check for correct phone number
    var rePhoneNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
    var rePhoneNumber2 = new RegExp(/^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/);
    
    if (rePhoneNumber.test(s.trim()) || rePhoneNumber2.test(s.trim())) 
        return true;

	return false;
}

/**
 * Cookie Stuff
 */ 
 // Get a cookie
function getCookie(name) 
{
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

// Deletes the cookie
function deleteCookie(name, path, domain) 
{
	if( getCookie( name ) ) 
		document.cookie = name + "=" + ( ( path ) ? ";path=" + path : "") +	( ( domain ) ? ";domain=" + domain : "" ) +	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}	 

// Add a cookie
function setCookie(name, value, expires, path, domain, secure) 
{
	var today = new Date();
	today.setTime( today.getTime() );
	
	if(expires)
		expires = expires * 1000 * 60 * 60 * 24;

	var expires_date = new Date(today.getTime() + (expires));
	
	document.cookie = name + '=' +escape( value ) +
	( ( expires ) ? ';expires=' + expires_date.toGMTString() : '') + 
	( ( path ) ? ';path=' + path : '') + 
	( ( domain ) ? ';domain=' + domain : '') +
	( ( secure ) ? ';secure' : '');
}


var reWhitespace = /^\s+$/
	var reLetter = /^[a-zA-Z]$/
	var reAlphabetic = /^[a-zA-Z\s]+$/
	var reAlphanumeric = /^[a-zA-Z0-9]+$/
	var reStreetAddress = /^[a-zA-Z0-9\s_\-#]+$/
	var reEmailAddress = /^[a-zA-Z0-9\s_\-#@\.]+$/
	var reDigit = /^\d/
	var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/
	var reLetterOrDigit = /^([a-zA-Z]|\d)$/
	var reInteger = /^\d+$/
	var reUpCaseLetter = /^[A-Z]$/
	var rePolicyNumber = /^[a-zA-Z]{2,3}-\d{7}-\d{2}$/  //example: AS-1234567-08 or UCA-1234567-08

	var digits = "0123456789";
	var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
	var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	var USStateCodeDelimiter = "|";
	var USStateCodes="AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY|AE|AA|AE|AE|AP"

	var whitespace = "\t\n\r";
	var digitsInZIPCode1 = 5;
	var digitsInZIPCode2 = 9;

	var mPrefix = "The ";
	var mSuffix = " field is required. Please enter it now.";
	var mSuffix1 = " field input is empty or invalid, please re-enter it now.";

	function makeArray(n) {
	   for (var i = 1; i <= n; i++) { this[i] = 0; }
	    return this
	}

	var daysInMonth = makeArray(12);
	daysInMonth[1] = 31;
	daysInMonth[2] = 29;   
	daysInMonth[3] = 31;
	daysInMonth[4] = 30;
	daysInMonth[5] = 31;
	daysInMonth[6] = 30;
	daysInMonth[7] = 31;
	daysInMonth[8] = 31;
	daysInMonth[9] = 30;
	daysInMonth[10] = 31;
	daysInMonth[11] = 30;
	daysInMonth[12] = 31;


	function replaceQuote(s) {
		var dqt = " ";
		var sqt = " ";
		s = s.replace(/"/g, dqt);
		s = s.replace(/'/g, sqt);

		return s;
	}

	function replaceQuote_1(s) {
		var dqt = " ";
		var sqt = " ";
		s = s.replace(/"/g, dqt);
		s = s.replace(/'/g, sqt);
		s = s.replace(/</g, '');
		s = s.replace(/>/g, '');

		return s;
	}

	function isEmpty(s) {
	   return ((s == null) || (s.length == 0))}

	function isE(s) {
	      return (isEmpty(s) || reWhitespace.test(s));}
	      
	function isNotE(s) {
		return !(isEmpty(s) || reWhitespace.test(s));}      

	function stripCharsInBag (s, bag) {
	   var i;
	   var returnString = "";
	   for (i = 0; i < s.length; i++)    {   
	   	var c = s.charAt(i);
	        if (bag.indexOf(c) == -1) returnString += c;
	    }
	    return returnString;
	}

	function charInString (c, s) {   for (i = 0; i < s.length; i++)
	    {   if (s.charAt(i) == c) return true;
	    }
	    return false
	}

	function isStreetAddress(s)
	{
		return reStreetAddress.test(s);		
	}
	
	function isEmailAddress(s)
	{
		return reEmailAddress.test(s);		
	}
	
	
	function stripWhitespace (s) {
	   return stripCharsInBag (s, whitespace)}

	function stripInitialWhitespace (s) {
	   var i = 0;
	    while ((i < s.length) && charInString (s.charAt(i), whitespace))
	       i++;
	    return s.substring (i, s.length);
	}

	//Remove initial 0
	function stripZ (s) {
	   var i = 0;
	    while ((i < s.length) && (charInString (s.charAt(i), '0')))
	       i++;
	    return s.substring (i, s.length);
	}

	function stripDashIn (s) {
	    if (s.charAt(0)=='-' || s.charAt(s.length-1)=='-' || s.indexOf('--')!=-1)
	    return s;
		return stripCharsInBag (s, '-');
	}

	function isName (s) 
	{
		return (isAlphabetic(stripWhitespace(s))) //(isAlphabetic(stripWhitespace(stripDashIn(s))))
	}

	function isLetter (c) {
	   return reLetter.test(c)}

	function isUpCaseLetter (c) {
	   return reUpCaseLetter.test(c)}

	function isDigit (c) {
	   return (reDigit.test(c))&&(c.length==1)}

	function isLetterOrDigit (c) {
	   return reLetterOrDigit.test(c)}

	function isInteger (s) {
	    return reInteger.test(s)}

	function isFloat (s) {
		if(s.charAt(0)=='-') return reFloat.test(s.substring(1))
	    return reFloat.test(s)}

	function isAlphabetic (s) {
	       return reAlphabetic.test(s)}
	       
	function isNumeric (s) {
	       return reDigit.test(s)}

	function isAlphanumeric (s) {
	       return reAlphanumeric.test(s)}

	function isNotAlphabetic(s) {
		return (!isAlphabetic(stripWhitespace(s))&&!isEmpty(stripInitialWhitespace(s)))}

	function isZIPCode (s) {
		s=stripInitialWhitespace(s);
	   return (isInteger(s) && (s.length == digitsInZIPCode1)) ||
	          ((s.length==10) && isInteger(s.substring(0,4))&& isInteger(s.substring(6,9)) && s.substr(5,1)=='-' )       
	}

	function isYear (s) {
	    return (isTwoDigits(s) || isFourDigits(s));}

	function isIntegerInRange (s, a, b) {
	    if (!isInteger(s)) return false;
	    var num = parseInt (s);
	    return ((num >= a) && (num <= b));
	}

	function isMonth (s) {
		ss=stripInitialWhitespace(s);
		sss=stripZ(ss);
	    return isIntegerInRange (sss, 1, 12) && ss.length<=2;
	}

	function isDay (s) {
		ss=stripInitialWhitespace(s);
		sss=stripZ(ss);
	       return isIntegerInRange (sss, 1, 31) && ss.length<=2;
	}

	function daysInFebruary (year) {
	       return(((year % 4 == 0)&&((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );}

	function isDate (year, month, day) {
		month=stripZ(month);
		day=stripZ(day);   
	    var intYear = parseInt(year);
	    var intMonth = parseInt(month);
	    var intDay = parseInt(day); 
	    if (intDay > daysInMonth[intMonth]) return false;
	     if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;
	    return true;
	}

	function Add_1_yr(DtStr) {
		DtArray = new Array();
		DtArray = DtStr.split("/");
		DtArray[2]= parseInt(DtArray[2])+1;
		if ( !isDate(DtArray[2],DtArray[0],DtArray[1]) ) {
			DtArray[1]= DtArray[1]-1;}
		return DtArray[0] + "/" + DtArray[1] + "/" + DtArray[2];
	}

	//valid day, mon, yesr, but invalid date
	function isNotDate(year,month,day){
		month=stripZ(month);
		day=stripZ(day);
		return (isYear(year)&&isMonth(month)&&isDay(day)&&(!isDate(year,month,day)))}

	function isNotDate1(year,month,day){
		month=stripZ(month);
		day=stripZ(day);
		return ((isYear(year)&&isMonth(month)&&isDay(day)&&(!isDate(year,month,day)))||
			  (!isYear(year)&&!isE(year))||(!isMonth(month)&&!isE(month))||(!isDay(day)&&!isE(day)))
	}

	function isStateCode(s){
	    return ((USStateCodes.indexOf(s) != -1) && (s.indexOf(USStateCodeDelimiter) == -1))}
	    
	function isStates(s){
	    var r = /^\s*([A-Z]{2}\s)*([A-Z]{2})+\s*$/;
	    
	    var sArray = s.split(' ');
	    
	    if(r.test(s)) {
	    	for (var i=0; i<sArray.length; i++){
	    		if(isE(sArray[i])) continue;
	    		else {if (!isStateCode(sArray[i])) return false;}
	    	}
	    	return true;
	    }
	    return false;
	}

//	function isEmail(s){
//		var r = /^[a-zA-Z0-9](.+)@[a-zA-Z0-9](.+)$/;
//	   	return r.test(s);
//	}

	function isPolicyNumber(s) {

		return rePolicyNumber.test(s);
	}
	//function isPhone(s){
//		var s = stripInitialWhitespace (s);
//		var r = /^((\({1}\s?\d{3}\s?\){1}\s?)|(\d{3}\s?-?\s?))\d{3}\s?-?\s?\d{4}$/;
//		return r.test(s);
	//}

	function isPhone(s){
		var	phone = "";
		for(var i=0; i<s.length; i++) {
			atI = s.charAt(i);
			if(isDigit(atI)) phone += atI;
		}
		if(phone.length==10) return true;
		return false;
	}


	function isSSN(s){
		var s = stripInitialWhitespace (s);
		var r = /^\d{3}\s?-?\s?\d{2}\s?-?\s?\d{4}$/;
		return r.test(s);
	}

	function isDateStr(s) {
		var r = /^\d{1,2}\/{1}\d{1,2}\/{1}(\d{4}|\d{2})$/;
	    var sArray = s.split('/');
		if(r.test(s)&&isMonth(sArray[0])&&isDay(sArray[1])) {
			return isDate(sArray[2],sArray[0],sArray[1]);
		}
		return false;
	} 

	function isNum(s){
		var s = stripInitialWhitespace (s);
		var r = /^((\d+)|(\d{1,3}(,{1}\d{3})+))$/;
		return r.test(s);
	}

	function isRatingFactor(s)
	{
		var r = /(^1\.)\d{2}$/;
		return r.test(s);
	}

	function isTwoDigits(s){
		s=stripInitialWhitespace(s);
		return (isInteger(s)&& s.length==2)
	}

	function isThreeDigits(s){
		s=stripInitialWhitespace(s);
		return (isInteger(s)&& s.length==3)
	}

	function isFourDigits(s){
		s=stripInitialWhitespace(s);
		return (isInteger(s)&& s.length==4)
	}


	function isExtension(s){
		return (isTwoDigits(s)||isThreeDigits(s)||isFourDigits(s)||isE(s))}

	function isMiddleName(s){
		return (isEmpty(stripWhitespace(s))||isAlphabetic(s)||(isUpCaseLetter(s.substr(0,1))&&s.substr(1,1)=='.'));}

	function isNotMiddleName(s){
		return(!isMiddleName(stripWhitespace(s))&&!isEmpty(stripWhitespace(s)))}

	//function isE(s){
//		return(isEmpty(stripWhitespace(s)))}

	function isObject(theField){
		type = typeof theField;
		if(type=='object') return true;
		else return false
	}

	function getRadioValue (radio) {   
		for (var i = 0; i <= radio.length; i++)
	    {  if (i==radio.length) { break } 
	       if (radio[i].checked) { break }
	    }
		 if (i==radio.length) {return false}
	    	 else   return radio[i].value
	}

	function isRadioChecked (radio) { 
		for (var i = 0; radio!=null && i <= radio.length; i++)
	    {  if (i==radio.length) { return false } 
	       if (radio[i].checked) { return true }
	    }
	}

	//return radio checked index
	function checkedIndex (radio) {   
		for (var i = 0; i <= radio.length; i++) {if(radio[i].checked) return i;}
	   	return "";
	}

	function uncheckRadio (radio) { 
		if(isRadioChecked(radio)) {
			for (var i = 0; i <= radio.length; i++) {
				if (radio[i].checked) { 
					radio[i].checked=false; 
					break;
				}
			}
		}
	}   

	function confirmReset() 
	{
		if (window.confirm('Are you sure that you want to clear all the fields input?'))
			F.reset();
	}

	function addHeadZero(s)
	{
		if(!isE(s))
		{
			while (s.substr(0,1) == "0") 
				s=s.substr(1);
		
			s = parseInt(s);
			
			if (isNaN(s)) 
				return '';
				
			if (s>=1 && s<=9) 
				s="0"+s;
		
			return s;
		}
		
		return '';
	}

	
	/*
	 * Auto Tab
	 */		
	var isNN = (navigator.appName.indexOf("Netscape") != -1);

	function autoTab(input,len, e) 
	{
		var keyCode = (isNN) ? e.which : e.keyCode; 
	  	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	  	
	  	if(input.value.length >= len && !containsElement(filter, keyCode)) 
	  	{
	    	input.value = input.value.slice(0, len);
	    	input.form[(getIndex(input)+1) % input.form.length].focus();
	  	}

	  	function containsElement(arr, ele) 
	  	{
	    	var found = false, index = 0;
	    	
		    while(!found && index < arr.length)
	    		if(arr[index] == ele)
	    			found = true;
		    	else
		    		index++;
	    		
	    	return found;
	  	}	

	  	function getIndex(input) 
	  	{
	    	var index = -1, i = 0, found = false;
	    	
	    	while (i < input.form.length && index == -1)
	    		if (input.form[i] == input)
	    			index = i;
	    		else i++;
	    		
	    	return index;
	  	}
	  	
	  	return true;
	}



	function showCalendarWidget(targetFieldId)
	{		
		var leftCoord = window.event.screenX; 
		var topCoord = window.event.screenY - 20;
		var feature = 'width=150,height=168,left=' + leftCoord +',top=' + topCoord + ',scrollbars=no'
		
		window.open('../HelperPages/calendar.jsp?id='+ targetFieldId, 'dobWin', feature); 
		
		return false;		
	}

	function isNotZero (s)
	{
		return (s !=0 );
	}

	String.prototype.trim = function() 
	{
		return this.replace(/^\s+|\s+$/g,"");
	}

	String.prototype.ltrim = function() 
	{
		return this.replace(/^\s+/,"");
	}

	String.prototype.rtrim = function() 
	{
		return this.replace(/\s+$/,"");
	}
